﻿//-------------------------------------------------------------------------------------
// All Rights Reserved , Copyright (C) 2011 , Hairihan TECH, Ltd. 
//-------------------------------------------------------------------------------------

using System.Data;
using System.IO;
using System.Text;
using System.Web;
using System.Windows.Forms;
public class BaseExportCSV
{
    #region public static StringBuilder GetCSVFormatData(DataTable dataTable) 通过DataTable获得CSV格式数据
    /// <summary>
    /// 通过DataTable获得CSV格式数据
    /// </summary>
    /// <param name="dataTable">数据表</param>
    /// <returns>CSV字符串数据</returns>
    public static StringBuilder GetCSVFormatData(DataTable dataTable)
    {
        StringBuilder StringBuilder = new StringBuilder();
        // 写出表头
        foreach (DataColumn DataColumn in dataTable.Columns)
        {
            StringBuilder.Append(DataColumn.ColumnName.ToString() + ",");
        }
        StringBuilder.Append("\n");
        // 写出数据
        foreach (DataRowView dataRowView in dataTable.DefaultView)
        {
            foreach (DataColumn DataColumn in dataTable.Columns)
            {
                StringBuilder.Append(dataRowView[DataColumn.ColumnName].ToString() + ",");
            }
            StringBuilder.Append("\n");
        }
        return StringBuilder;
    }
    #endregion

    #region public static StringBuilder GetCSVFormatData(DataSet dataSet) 通过DataSet获得CSV格式数据
    /// <summary>
    /// 通过DataSet获得CSV格式数据
    /// </summary>
    /// <param name="dataSet">数据集</param>
    /// <returns>CSV字符串数据</returns>
    public static StringBuilder GetCSVFormatData(DataSet dataSet)
    {
        StringBuilder StringBuilder = new StringBuilder();
        foreach (DataTable dataTable in dataSet.Tables)
        {
            StringBuilder.Append(GetCSVFormatData(dataTable));
        }
        return StringBuilder;
    }
    #endregion

    #region public static void ExportCSV(DataTable dataTable, string fileName) 导出CSV格式文件
    /// <summary>
    /// 导出CSV格式文件
    /// </summary>
    /// <param name="dataTable">数据表</param>
    /// <param name="fileName">文件名</param>
    public static void ExportCSV(DataTable dataTable, string fileName)
    {
        StreamWriter StreamWriter = new StreamWriter(fileName, false, System.Text.Encoding.GetEncoding("gb2312"));
        StreamWriter.WriteLine(GetCSVFormatData(dataTable).ToString());
        StreamWriter.Flush();
        StreamWriter.Close();
    }
    #endregion

    #region public static void ExportCSV(DataSet dataSet, string fileName) 导出CSV格式文件
    /// <summary>
    /// 导出CSV格式文件
    /// </summary>
    /// <param name="dataSet">数据集</param>
    /// <param name="fileName">文件名</param>
    public static void ExportCSV(DataSet dataSet, string fileName)
    {
        StreamWriter StreamWriter = new StreamWriter(fileName, false, System.Text.Encoding.GetEncoding("gb2312"));
        StreamWriter.WriteLine(GetCSVFormatData(dataSet).ToString());
        StreamWriter.Flush();
        StreamWriter.Close();
    }
    #endregion

    #region public static void ExportCSV(DataGridView DataGridView, string fileName) 导出CSV格式文件
    /// <summary>
    /// 导出CSV格式文件
    /// </summary>
    /// <param name="DataGridView">表格控件</param>
    /// <param name="fileName">文件名</param>
    public static void ExportCSV(DataGridView DataGridView, string fileName)
    {
        StreamWriter streamWriter = new StreamWriter(fileName, false, System.Text.Encoding.GetEncoding("gb2312"));
        StringBuilder stringBuilder = new StringBuilder();
        // 写出表头
        for (int i = 0; i < DataGridView.Columns.Count; i++)
        {
            if (DataGridView.Columns[i].Visible)
            {
                stringBuilder.Append(DataGridView.Columns[i].HeaderText + ",");
            }
        }
        streamWriter.WriteLine(stringBuilder.ToString());
        // 写出数据
        foreach (DataRowView dataRowView in (DataView)DataGridView.DataSource)
        {
            stringBuilder = new StringBuilder();
            for (int i = 0; i < DataGridView.Columns.Count; i++)
            {
                if (DataGridView.Columns[i].Visible)
                {
                    stringBuilder.Append(dataRowView[DataGridView.Columns[i].DataPropertyName].ToString() + ",");
                }
            }
            streamWriter.WriteLine(stringBuilder.ToString());
        }
        streamWriter.Flush();
        streamWriter.Close();
    }
    #endregion


}